package com.jzo2o.customer.controller.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.jni.Address;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 31084
 */
@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 地址簿相关接口")
public class AddressBookController {
    @Resource
    private IAddressBookService addressBookService;

    /**
     * 小程序新增地址簿
     * @param dto 地址簿信息
     */
    @PostMapping()
    @ApiOperation("新增地址簿")
    public void addAddressBook(@RequestBody AddressBookUpsertReqDTO dto) {
        addressBookService.addAddressBook(dto);
    }

    /**
     * 分页查询地址簿
     * @param dto 分页查询请求参数
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询地址簿")
    public PageResult<AddressBookResDTO> page(AddressBookPageQueryReqDTO dto) {
       return addressBookService.page(dto);
    }

    /**
     * 根据id查询地址簿
     * @param id 地址簿id
     * @return 地址簿信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址簿")
    @ApiImplicitParam(name = "id", value = "地址簿id", required = true, dataTypeClass = Long.class)
    public AddressBookResDTO getById(@PathVariable("id") Long id) {
        return BeanUtil.copyProperties(addressBookService.getById(id), AddressBookResDTO.class);
    }

    /**
     * 更新地址簿
     * @param id 地址簿id
     * @param dto 地址薄修改提交
     */
    @PutMapping("/{id}")
    @ApiOperation("更新地址簿")
    @ApiImplicitParam(name = "id", value = "地址簿id", required = true, dataTypeClass = Long.class)
    public void update(@PathVariable("id") Long id, @RequestBody AddressBookUpsertReqDTO dto) {
        AddressBook addressBook = BeanUtil.copyProperties(dto, AddressBook.class);
        addressBook.setId(id);
        if (dto.getIsDefault()==1){
            addressBookService.update(new UpdateWrapper<AddressBook>()
                    .set("is_default",0)
                    .eq("is_default",1)
                    .eq("user_id", UserContext.currentUser().getId())
                    );
            addressBookService.updateById(addressBook);
        }
        addressBookService.updateById(addressBook);
    }

    /**
     * 地址簿批量删除
     * @param ids
     */
    @DeleteMapping("/batch")
    @ApiOperation("地址簿批量删除")
    public void deleteBatch(@RequestBody List<Long> ids) {
        addressBookService.deleteBatch(ids);
    }

    /**
     * 设置默认地址
     * @param flag 是否是默认地址
     * @param id 地址簿id
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public void setDefault(Integer flag, Long id) {
        addressBookService.setDefault(flag, id);
    }

    /**
     * 查询默认地址
     * @return 默认地址信息
     */
    @GetMapping("/defaultAddress")
    @ApiOperation("查询默认地址")
    public AddressBookResDTO getDefaultAddress() {
        return addressBookService.getDefaultAddress();
    }

}
